package fr.emse.tatiana.graphical;

import java.util.List;
import java.util.Vector;

import org.eclipse.core.resources.IContainer;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.Path;
import fr.emse.tatiana.display.BadDisplayFile;
import fr.emse.tatiana.display.DisplayFileReader;
import fr.emse.tatiana.display.DisplayFileWriter;
import fr.emse.tatiana.replayable.IReplayableModel;
import fr.emse.tatiana.replayable.IReplayableSaver;

public class DisplayRulesParser implements IReplayableSaver {
	public static final String DISPLAY_TYPE = "fr.emse.tatiana.scorelayout";

	private IFile file;
	private boolean issaved;
	private List<DisplayRule> rules;
	private IReplayableModel model; // back compat only
	private IContainer folder;

	public DisplayRulesParser(IContainer folder) {
		this.rules = new Vector<DisplayRule>();
		this.issaved = true;
		this.folder = folder;
	}
	
	public DisplayRulesParser(IFile file, IReplayableModel model) {
		this(file.getParent());
		this.file = file;
		this.model = model;
		openFile();
	}

	public void openFile() {
		if (file == null) {
			return;
		}
		try {
			file.refreshLocal(IResource.DEPTH_ZERO, null);
			if (file.exists()) {
				DisplayFileReader reader = new DisplayFileReader(file.getLocation().toString());
				if (!DISPLAY_TYPE.equals(reader.getType())) {
					throw new BadDisplayFile(BadDisplayFile.WRONGFILETYPE +" expected:"+DISPLAY_TYPE);
				}
				for (boolean notdone = reader.selectFirstItem();notdone;notdone = reader.selectNextItem()) {

					String facetname = reader.getStringValueNoFail("facetname");
					if (facetname==null) { // backward compat
						Integer facetint = reader.getIntegerValueNoFail("facet");
						if (facetint!=null) {
							if (model!=null && facetint < model.getFacetCount()) {
								facetname = model.getFacetName(facetint);
							} else {
								facetname = "BUG - DELETE THIS RULE"; // fuck 'em
							}
						}
					}
					String facetvalue = reader.getStringValueNoFail("facetvalue");
					String property = reader.getStringValueNoFail("property");
					String propertyvalue = reader.getStringValueNoFail("propertyvalue");
					rules.add(new DisplayRule(facetname,facetvalue,property,propertyvalue));
				}

			}
		} catch (CoreException e) {
			// shouldn't happen
			e.printStackTrace();
		} catch (BadDisplayFile e) {
			// shouldn't happen either
			e.printStackTrace();
		}

	}

	public List<DisplayRule> getRules() {
		return rules;
	}

	public void doSave() {
		if (file == null) {
			return;
		}
		DisplayFileWriter writer = new DisplayFileWriter(file.getLocation().toString(),DISPLAY_TYPE);

		for (DisplayRule rule : rules) {
			writer.newItem();
			if (rule.getFacet()!=null) {
				writer.newInfo("facetname", "",rule.getFacet());
			}
			if (rule.getFacetvalue()!=null) {
				writer.newInfo("facetvalue", "",rule.getFacetvalue());
			}
			if (rule.getProperty()!=null) {
				writer.newInfo("property", "",rule.getProperty());
			}
			if (rule.getPropertyvalue()!=null) {
				writer.newInfo("propertyvalue", "",rule.getPropertyvalue());
			}

		}
		writer.close();
		issaved = true;
	}

	public void setFile(String value) {
		file = folder.getFile(new Path(value));
	}
	
	public void doSave(String value) {
		setFile(value);
		doSave();
	}

	public String getName() {
		return file.getName(); // don't allow dosave as or stuff like that
	}

	public boolean hasFile() {
		return file!=null; // don't allow dosave as or stuff like that
	}

	public boolean isSaved() {
		return issaved;
	}

	public void touch() {
		this.issaved = false;
	}

	public IProject getProject() {
		return folder.getProject();
	}

	public void close() {
		// nothing to do on close
		
	}


}
